package com.study.ChantGpt;

import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author yangyanbin
 * @since 20221209
 **/
public class ChangePic {
    @Test
    public void changeImage() throws IOException {
        // 1、先加载背景图片和二维码图片。
        BufferedImage backgroundImage = ImageIO.read(new File("src/main/resources/背景图.png"));
        BufferedImage qrCodeImage = ImageIO.read(new File("src/main/resources/img.png"));

        // 2、创建一个新图片，用于合成背景图片和二维码图片
        int width = backgroundImage.getWidth();
        int height = backgroundImage.getHeight();
        BufferedImage mergedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);


        // 3、获取背景图片和二维码图片的图像画笔，并将背景图片绘制到新图片上。
        Graphics2D g2d = mergedImage.createGraphics();
        g2d.drawImage(backgroundImage, 0, 0, null);

        // 4、计算二维码图片的绘制位置，并将二维码图片绘制到新图片上。
        int x = ((width - qrCodeImage.getWidth()) / 2) + 160;
        int y = (height - qrCodeImage.getHeight()) - 50;
        g2d.drawImage(qrCodeImage, x, y, null);

        // 5、释放图像画笔的资源
        g2d.dispose();


        // 6、保存合成后的图片
        ImageIO.write(mergedImage, "png", new File("src/main/resources/change.png"));

    }
}
